package com.define.commons.dao.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.define.commons.dao.dao.BaseDao;
import com.define.commons.dao.service.FastMybatisBaseService;
import com.gitee.fastmybatis.core.query.Query;

/**
 * 根据泛型提供基础 CRUD 的 ServiceImpl
 * @Author xiewenlong
 * @Date 2018/10/31 12:49
 */
public class FastMybatisServiceImpl<T, I> implements FastMybatisBaseService<T, I> {

	/**
     * 启动Ehcache缓存  时 name需要用这个
     * value属性表示使用哪个缓存策略，缓存策略在Ehcache.xml配置
     */
    public static final String CACHE_NAME = "serviceCache";

    @Autowired
    private BaseDao<T, I> mapper;

    /**
     * 在具体的 实现类 开启缓存 如下  1.先在具体 服务消费者 的application.java上 启用 缓存  @EnableCaching，2 在serviceImpl具体业务实现方法启用缓存
     * @CacheEvict(value = "实体类别名")  清除缓存
     */
//    @CacheEvict(value = CACHE_NAME,allEntries=true)
    @Override
    public int delete(T t) {
        return mapper.delete(t);
    }

    /**
     * @CacheEvict(value="实体类别名",key="#实体类别名.id")
     * 根据id 清除缓存 ， 例如：@CacheEvict(value = "user",key="#user.id")
     */
//    @CacheEvict(value = CACHE_NAME ,key="#i")  
    @Override
    public int deleteById(I i) {
        return mapper.deleteById(i);
    }

    @Override
    public int deleteByQuery(Query query) {
        return mapper.deleteByQuery(query);
    }

    /**
     * @CacheEvict(value = CACHE_NAME)
     */
    @Override
    public int save(T t) {
        return mapper.save(t);
    }

    @Override
    public int saveIgnoreNull(T t) {
        return mapper.saveIgnoreNull(t);
    }

    @Override
    public int saveBatch(List<T> list) {
        return mapper.saveBatch(list);
    }

    @Override
    public int saveMulti(List<T> list) {
        return mapper.saveMulti(list);
    }

    @Override
    public int saveMultiSet(List<T> list) {
        return mapper.saveMultiSet(list);
    }

    /**
     * @Cacheable(value="实体类别名",key="#实体类别名.id")
     * 添加  缓存
     */
//    @Cacheable(value = CACHE_NAME ,key= "#i")
    @Override
    public T getById(I i) {
        return mapper.getById(i);
    }

    @Override
    public T getByQuery(Query query) {
        return mapper.getByQuery(query);
    }

    @Override
    public T getByColumn(String s, Object o) {
        return mapper.getByColumn(s, o);
    }

    @Override
    public long getCount(Query query) {
        return mapper.getCount(query);
    }

    @Override
    public List<T> listByColumn(String s, Object o) {
        return mapper.listByColumn(s, o);
    }

    @Override
    public List<T> list(Query query) {
        return mapper.list(query);
    }

    @Override
    public List<Map<String, Object>> listMap(List<String> list, Query query) {
        return mapper.listMap(list, query);
    }

    /**
     * @CachePut(value="实体类",key="#实体类.id")
     * 更新缓存
     */
//    @CacheEvict(value = CACHE_NAME,allEntries=true)
    @Override
    public int update(T t) {
        return mapper.update(t);
    }

    /**
     * @CachePut(value="实体类",key="#实体类.id")
     * 更新缓存
     */
//	@CacheEvict(value = CACHE_NAME,allEntries=true)
    @Override
    public int updateIgnoreNull(T t) {
        return mapper.updateIgnoreNull(t);
    }

    @Override
    public int updateByQuery(Object o, Query query) {
        return mapper.updateByQuery(o, query);
    }

}
